
# 拦截器，实现统一登录校验拦截
import re

from django.http import HttpResponseRedirect, HttpResponse
from django.utils.deprecation import MiddlewareMixin

from entity.common.response import ResponseData, ResponseStatus
from utils.transfer import transferToJson


class SimpleMiddleware(MiddlewareMixin):
    exclude_paths = ['/user/loginVerify','/user/userRegister','/user/emailCode']
    def process_request(self, request):
        print("这是拦截器")
        print(request.path)
        # user路径的需要拦截
        if re.match(r"/user/",request.path)  and request.path not in SimpleMiddleware.exclude_paths:
            if request.session.get('user', None):
                print("通过拦截")
                pass
            else:
                print("用户没有登录")
                response = ResponseData(success=False,responseCode=ResponseStatus.USERINFO_ERROR,message="请登录",data={})
                return  HttpResponse(transferToJson(response), content_type="application/json;charset=UTF-8")
